// src/user/user.module.ts
import { MiddlewareConsumer, Module, NestModule, RequestMethod } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './user.entity';
import { UserService } from './user.service';
import { UserController } from './user.controller';
import { RedisService } from 'src/redis/redis.service';
import { RateLimiterMiddleware } from 'src/middleware/rate-limiter.middleware';

@Module({
  imports: [
    TypeOrmModule.forFeature([User]),
  ],
  providers: [UserService,RedisService],
  controllers: [UserController],
  exports: [UserService], 
})
export class UserModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    // consumer.apply(RateLimiterMiddleware).forRoutes('/user/login'); // 应用于 login 路由
    consumer
      .apply(RateLimiterMiddleware)
      .forRoutes({ path: '/user/*', method: RequestMethod.POST }); // 对 /user 下的所有 POST 请求应用限流
  }
}